<script type="text/javascript">
var $datatable;
var _DATATABLE_TR;
var oTable;
var FLAG_ADD_CLICK = true;
var FLAG_EDIT_CLICK = true;
var FLAG_DELETE_CLICK = true;
var isEDIT = false;
<?php if( has_permission('System.Permission.Update') ): ?>
    isEDIT = true;
<?php endif; ?>
    
$(document).ready(function() {
    $datatable = $('#datatable_dvt');
    $dialog_form_edit = $('#dialog_form_edit');
    $dialog_form_add = $('#dialog_form_add');
    $dialog_confrim_delete = $('#dialog_confrim_delete');
    
    oTable = $datatable.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        //"bFilter": false,
        "sAjaxSource": $datatable.attr('active_url'),
        "fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            datatable_selected();
            _DATATABLE_TR = null;
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if( aData['id'] != undefined ){
                $(nRow).attr("value_id",aData['id']);
            }
            if( aData['modify'] != undefined ){
                $(nRow).attr("modify",aData['modify']);
            }
            //if ( aData[4] == "A" ){$('td:eq(4)', nRow).html( '<b>A</b>' );}
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    dialogFormAddNew();
    dialogConfrimDelete();
    dialogFormEdit();
    dialogError();
} );

function datatable_selected(){
    $datatable.find('tbody tr[modify=edit]').click( function( e ) {
        if ( $(this).hasClass('row_selected') ) {
            $(this).removeClass('row_selected');
            _DATATABLE_TR = null;
        }else {
            oTable.$('tr.row_selected').removeClass('row_selected');
            $(this).addClass('row_selected');
            _DATATABLE_TR = $(this).attr('value_id');
        }
    });
    
    $datatable.find('tbody tr[modify=edit]').bind("dblclick", function(){
        if ( !$(this).hasClass('row_selected') ) {
            $(this).addClass('row_selected');
        }
        _DATATABLE_TR = $(this).attr('value_id');
        even_edit_dbclick( _DATATABLE_TR );

    });

}

function even_edit_dbclick(id){
    if(isEDIT == true){
        $('#content-loading').height($('#content-warpp').outerHeight());
        $('#content-loading').find('img').css({'top':'50%','left':'50%','margin-top':'-100px','margin-left':'-100px'});
        $('#content-loading').fadeIn(200,function(){
            var url = $dialog_form_edit.attr('active_url') + '/' + id;
            $.get(url, function(data) {
                $dialog_form_edit.find('.dialog-content').html( data );
                $dialog_form_edit.dialog( "open" );
                $('#content-loading').delay(300).fadeOut(200,function(){
                    //$('#product-list').fadeIn(200);
                    //FLAG_WARP_LEFT_CLICK = true;
                });
            });
        });
    }
}

function dialogFormAddNew(){
    
    $dialog_form_add.dialog({
        autoOpen: false,
        //height: 300,
        width: 420,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                if(FLAG_ADD_CLICK == true){
                    FLAG_ADD_CLICK = false;
                    $dialog_form_add.find('.dialog-content').fadeOut(200,function(){
                        $dialog_form_add.find('.dialog-loading').fadeIn(200,function(){
                            $("#frm_add").ajaxSubmit({
                                dataType:  'json',
                                /*beforeSubmit: function(formData, jqForm, options) {
                                    return validate_frm();
                                },*/
                                success: function(data) {
                                    if(data.flag == true){
                                        //oTable.fnDraw();
                                        //oTable.fnFilter( data.ten );
                                        oTable.fnSort( [ [0,'desc'] ] );
                                        $this.dialog( "close" );
                                        $dialog_form_add.find('.dialog-loading').hide();
                                        $dialog_form_add.find('.dialog-content').html('').show();
                                        FLAG_ADD_CLICK = true;
                                    }else{
                                        $("input.i-error").removeClass("i-error");
                                        $("select.i-error").removeClass("i-error");
                                        $("div.error").hide();
                                        $.each(data.error, function(key, value) {
                                            $("input[name="+value.name+"]").addClass("i-error");
                                            $("select[name="+value.name+"]").addClass("i-error");
                                            $("#error_"+value.name).html(value.message);
                                            $("#error_"+value.name).show();
                                        });
                                        $dialog_form_add.find('.dialog-loading').delay(100).fadeOut(200,function(){
                                            $dialog_form_add.find('.dialog-content').fadeIn(200);
                                            FLAG_ADD_CLICK = true;
                                        });
                                    }
                                }
                            });
                        });
                    });
                }
                
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_add.find('.dialog-content').html('');
            FLAG_ADD_CLICK = true;
        }
    });
        
}

function dialogFormEdit(){
    
    $dialog_form_edit.dialog({
        autoOpen: false,
        //height: 300,
        width: 420,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                if(FLAG_EDIT_CLICK == true){
                    FLAG_EDIT_CLICK = false;
                    $dialog_form_edit.find('.dialog-content').fadeOut(200,function(){
                        $dialog_form_edit.find('.dialog-loading').fadeIn(200,function(){
                            $("#frm_add").ajaxSubmit({
                                dataType:  'json',
                                /*beforeSubmit: function(formData, jqForm, options) {                        
                                    return validate_frm();
                                },*/
                                success: function(data) {
                                    if(data.flag == true){
                                        var $tr = oTable.$('tr[value_id="'+ data.id +'"]');
                                        $tr.find('td:eq(1)').html(data.supplier_name);
                                        $tr.find('td:eq(2)').html(data.supplier_note);
                                        $tr.find('td:eq(3)').html(data.status);
                                        $this.dialog( "close" );
                                        $dialog_form_edit.find('.dialog-loading').hide();
                                        $dialog_form_edit.find('.dialog-content').html('').show();
                                        FLAG_EDIT_CLICK = true;
                                    }else{
                                        $("input.i-error").removeClass("i-error");
                                        $("select.i-error").removeClass("i-error");
                                        $("div.error").hide();
                                        $.each(data.error, function(key, value) {
                                            $("input[name="+value.name+"]").addClass("i-error");
                                            $("select[name="+value.name+"]").addClass("i-error");
                                            $("#error_"+value.name).html(value.message);
                                            $("#error_"+value.name).show();                                 
                                        });
                                        $dialog_form_edit.find('.dialog-loading').delay(100).fadeOut(200,function(){
                                            $dialog_form_edit.find('.dialog-content').fadeIn(200);
                                            FLAG_EDIT_CLICK = true;
                                        });
                                    }
                                }
                            });
                        });
                    });
                }
                
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_edit.find('.dialog-content').html('');
            FLAG_EDIT_CLICK = true;
        }
    });
        
}

function validate_frm(){
    var flag = true;
    var name = $('input[name=supplier_name]').val();
    if(name == ''){
        $("input[name=supplier_name]").addClass("i-error");
        $("#iname").show();
        flag = false;
    }else{
        $("#iname").hide();
        $("input[name=supplier_name]").removeClass("i-error");
    }
    
    return flag;
}

function dialogConfrimDelete(){
    
    $dialog_confrim_delete.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "Delete": function() {
                $('#content-loading').height($('#content-warpp').outerHeight());
                $('#content-loading').find('img').css({'top':'50%','left':'50%','margin-top':'-100px','margin-left':'-100px'});
                $('#content-loading').fadeIn(200,function(){
                    var url = $dialog_confrim_delete.attr('active_url');
                    $.post(url, { id: _DATATABLE_TR },function(data){
                        if(data.flag == true){
                            _DATATABLE_TR = null;
                            oTable.fnDraw();
                        }else{
                            alert(data.message);
                        }
                        $('#content-loading').delay(300).fadeOut(200,function(){
                            //$('#product-list').fadeIn(200);
                            //FLAG_WARP_LEFT_CLICK = true;
                        });
                    }, "json");
                });
                
                /*var url = $dialog_confrim_delete.attr('active_url');
                
                $.post(url, { id: _DATATABLE_TR },function(data){
                     _DATATABLE_TR = null;
                     oTable.fnDraw();
                }, "json");*/
                
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            //allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
        
}

function even_add(){
    $('#content-loading').height($('#content-warpp').outerHeight());
    $('#content-loading').find('img').css({'top':'50%','left':'50%','margin-top':'-100px','margin-left':'-100px'});
    $('#content-loading').fadeIn(200,function(){
        var url = $dialog_form_add.attr('active_url');
        $.get(url, function(data) {
            $dialog_form_add.find('.dialog-content').html( data );
            $dialog_form_add.dialog( "open" );
            $('#content-loading').delay(300).fadeOut(200,function(){
                //$('#product-list').fadeIn(200);
                //FLAG_WARP_LEFT_CLICK = true;
            });
            
        });
        
    });
    
    /*var url = $dialog_form_add.attr('active_url');
    $.get(url, function(data) {
        $dialog_form_add.html( data );
        $dialog_form_add.dialog( "open" );
    });*/
}

function even_edit(){
    if(isEDIT == true){
        if(_DATATABLE_TR != null){
            $('#content-loading').height($('#content-warpp').outerHeight());
            $('#content-loading').find('img').css({'top':'50%','left':'50%','margin-top':'-100px','margin-left':'-100px'});
            $('#content-loading').fadeIn(200,function(){
                var url = $dialog_form_edit.attr('active_url') + '/' + _DATATABLE_TR;
                $.get(url, function(data) {
                    $dialog_form_edit.find('.dialog-content').html( data );
                    $dialog_form_edit.dialog( "open" );
                    $('#content-loading').delay(300).fadeOut(200,function(){
                        //$('#product-list').fadeIn(200);
                        //FLAG_WARP_LEFT_CLICK = true;
                    });
                });
            });
            
            /*var url = $dialog_form_edit.attr('active_url') + '/' + _DATATABLE_TR;
            $.get(url, function(data) {
                $dialog_form_edit.html( data );
                $dialog_form_edit.dialog( "open" );
            });*/
        }else{
            $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
            $dialog_error.dialog( "open" );
        }
    }
}

function even_delete(){
    if(_DATATABLE_TR != null){
        $dialog_confrim_delete.dialog( "open" );
    }else{
        $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
        $dialog_error.dialog( "open" );
    }
}
</script>
<div id="content-loading" style="display: none;">
    <img class="icon_loading_2" src="<?php print base_url('template/ess/images/loading/loading_3.gif') ?>" alt="loading...">
</div>
<div id="content-warpp">
    <div class="menu-button">
        <div class="message"></div>
        <?php if( has_permission('KHVT.Supplier.Create') ): ?>
        <button onclick="even_add();"><?php print lang('btn_new'); ?></button>
        <?php endif; ?>
        <?php if( has_permission('KHVT.Supplier.Update') ): ?>
        <button onclick="even_edit();"><?php print lang('btn_edit'); ?></button>
        <?php endif; ?>
        <?php if( has_permission('KHVT.Supplier.Delete') ): ?>
        <button onclick="even_delete();"><?php print lang('btn_delete'); ?></button>
        <?php endif; ?>
        <div class="clear"></div>
    </div>

    <div class="warpp">
        <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_dvt" active_url="<?php print site_url('vt/datatable_supplier'); ?>">
            <thead>
                <tr>
                    <th width="40px"><?php print lang('vt_stt'); ?></th>
                    <th><?php print lang('vt_suppliername'); ?></th>
                    <th><?php print lang('vt_note'); ?></th>
                    <th><?php print lang('vt_trangthai'); ?></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td colspan="5" class="dataTables_empty">Loading data from server</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<div id="dialog_form_add" title="<?php print lang('vt_title_addsupplier'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_supplier/'); ?>">
    <div class="dialog-loading" style="display: none;">
        <img class="icon_loading_3" src="<?php print base_url('template/ess/images/loading/loading_5.gif') ?>" alt="loading...">
    </div>
    <div class="dialog-content"></div>
</div>
<div id="dialog_form_edit" title="<?php print lang('vt_title_editsupplier'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_supplier/'); ?>">
    <div class="dialog-loading" style="display: none;">
        <img class="icon_loading_3" src="<?php print base_url('template/ess/images/loading/loading_5.gif') ?>" alt="loading...">
    </div>
    <div class="dialog-content"></div>
</div>
<div id="dialog_confrim_delete" title="<?php print lang('title_thongbao'); ?>" active_url="<?php print site_url('vt/delete_supplier/'); ?>">
    <p><?php print lang('msg_delete'); ?></p>
</div>